package com.sqlist.shareserver.service.impl;

import com.sqlist.shareserver.domain.User;
import com.sqlist.shareserver.mapper.UserMapper;
import com.sqlist.shareserver.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public User register(User user) {
        try {
            User registerUser = (User) user.clone();
            registerUser.setPassword(null);
            registerUser = userMapper.selectOne(user);
            if (registerUser == null) {
                log.info("{} 不存在重复", user.getUsername());
                userMapper.insert(user);
                return userMapper.selectOne(user);
            } else {
                return user;
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        return user;
    }

    @Override
    public User login(User user) {
        return userMapper.selectOne(user);
    }
}
